<template>
  <div class="app-container">
    <div class="filter-container" >
      <el-select v-model="value" placeholder="选择类型" class="filter-item" style="float: left;">
         <el-option style="text-align: center;" v-for="goodType in typeList" :key="goodType.typeId" :label="goodType.goodsType" :value="goodType.typeId"></el-option>
      </el-select>
      <el-button v-waves class="filter-item" style="float: left;margin-left: 10px;" type="success" icon="el-icon-zoom-in" @click="handleType">
        查询
      </el-button>
      <el-input  v-model="searchname" placeholder="elasticSearch根据资料名或描述搜索" style="width: 400px;margin-right: 10px;margin-left: 30px;" class="filter-item" @keyup.enter.native="handleFilter" />
        <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="handleEs">
        搜索
      </el-button>
      <el-button v-waves :loading="downloadLoading" class="filter-item" type="danger" icon="el-icon-download" @click="handleDownload">
        导出技术点
      </el-button>
       <el-button v-if="checkPermission([1])" style="float: right;" class="filter-item" type="warning" icon="el-icon-plus" @click="handleCreate">
         添加类型
       </el-button>
    </div>

    <el-table
      v-loading="listLoading"
      :data="list"
      border
      fit
      highlight-current-row
      style="width: 100%;"

    >

      <el-table-column align="center" label="序号" width="95">
              <template slot-scope="row">
                {{ row.$index+1 }}
              </template>
            </el-table-column>
      <el-table-column label="资料图片" width="240px" align="center">
        <template slot-scope="{row}">
           <img :src="'http://139.159.144.167:80/'+row.images" style="width: 200px;height: 120px;">
       <!--   <span>{{ row.images }}</span> -->
        </template>
      </el-table-column>
      <el-table-column label="资料名称" width="200px" align="center">
        <template slot-scope="{row}">
          <span>{{ row.goodName }}</span>
        </template>
      </el-table-column>
      <el-table-column label="描述" width="300px" align="center">
        <template slot-scope="{row}">
          <span>{{ row.message}}</span>
        </template>
      </el-table-column>
      <el-table-column label="资料价格(¥)" width="150px" align="center">
        <template slot-scope="{row}">
          <span>{{ row.price }}</span>
        </template>
      </el-table-column>
      <el-table-column label="技术点报价(¥)" width="150px" align="center">
        <template slot-scope="{row}">
          <span>{{ row.fee }}</span>
        </template>
      </el-table-column>
      <el-table-column label="数量" width="150px" align="center">
        <template slot-scope="{row}">
          <span>{{ row.count}}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="400px" class-name="small-padding fixed-width">
        <template slot-scope="{row,$index}">
          <el-button type="primary" icon="el-icon-edit" size="mini" @click="handleUpdate(row)">
            修改
          </el-button>
          <el-button  type="success"  icon="el-icon-top"  size="mini" @click="handleUp(row)">
            上架
          </el-button>
          <el-button   type="warning"  icon="el-icon-bottom"  size="mini" @click="handleDown(row)">
            下架
          </el-button>
          <el-button  v-if="checkPermission([1])"  type="infor" icon="el-icon-upload" size="mini" @click="handleFiles(row)">
            绑定
          </el-button>
          <el-button v-if="checkPermission([1])"  icon="el-icon-delete"  size="mini" type="danger" @click="handleDelete(row,$index)">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />

    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible">
      <el-form ref="dataForm" :model="temp" label-position="left" label-width="110px" style="width: 400px; margin-left:50px;">
        <el-form-item v-if="dialogStatus==='update'" label="图片" prop="images">
           <img :src="'http://139.159.144.167:80/'+temp.images" style="width: 300px;height: 150px;">
        </el-form-item>
        <el-form-item v-if="dialogStatus==='up'" label="图片" prop="images">
           <img :src="'http://139.159.144.167:80/'+temp.images" style="width: 300px;height: 150px;">
        </el-form-item>
        <el-form-item v-if="dialogStatus==='down'" label="图片" prop="images">
           <img :src="'http://139.159.144.167:80/'+temp.images" style="width: 300px;height: 150px;">
        </el-form-item>
        <el-form-item v-if="dialogStatus==='update'" label="资料名称" prop="goodName">
          <el-input v-model="temp.goodName" placeholder="输入资料名称" />
        </el-form-item>
        <el-form-item v-if="dialogStatus==='file'" label="资料名称" prop="goodName">
          <el-input v-model="temp.goodName" placeholder="输入资料名称" />
        </el-form-item>
        <el-form-item v-if="dialogStatus==='file'" label="图片" prop="images">
           <img :src="'http://139.159.144.167:80/'+temp.images" style="width: 300px;height: 150px;">
        </el-form-item>
       <el-form-item v-if="dialogStatus==='update'" label="描述">
         <el-input v-model="temp.message" :autosize="{ minRows: 2, maxRows: 4}" type="textarea" placeholder="请输入描述信息" />
       </el-form-item>
      <el-form-item v-if="dialogStatus==='update'" label="资料价格(¥)" prop="price">
        <el-input v-model="temp.price" placeholder="输入资料价格" />
      </el-form-item>
      <el-form-item v-if="dialogStatus==='update'" label="技术点报价(¥)" prop="price">
        <el-input v-model="temp.function" placeholder="输入技术点报价" />
      </el-form-item>
      <el-form-item v-if="dialogStatus==='up'" label="上架(份)" prop="count">
        <el-input v-model="temp.up" placeholder="输入上架数量" />
      </el-form-item>
      <el-form-item v-if="dialogStatus==='down'" label="下架(份)" prop="count">
        <el-input v-model="temp.down" placeholder="输入下架数量" />
      </el-form-item>
      <el-form-item v-if="dialogStatus==='create'" label="类型名称" prop="goodsType">
        <el-input v-model="temp.goodsType" placeholder="输入类型名称" />
      </el-form-item>
      <el-form-item v-if="dialogStatus==='file'" label="选择文件" prop="fileName">
      <el-select v-model="value2" placeholder="选择文件" class="filter-item" style="float: left;">
         <el-option style="text-align: center;" v-for="files in fileList" :key="files.fileId" :label="files.fileName" :value="files.fileId"></el-option>
      </el-select>
       </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">
          取消
        </el-button>
        <el-button type="primary"  v-if="dialogStatus==='update'"  @click="updateData()">
          提交
        </el-button>
        <el-button type="primary" v-if="dialogStatus==='up'" @click="upData()">
          提交
        </el-button>
        <el-button type="primary" v-if="dialogStatus==='down'" @click="downData()">
          提交
        </el-button>
        <el-button type="primary" v-if="dialogStatus==='create'" @click="createData()">
          提交
        </el-button>
        <el-button type="primary" v-if="dialogStatus==='file'" @click="fileData()">
          提交
        </el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import { findGood1,findGood2,findAllType,search,getDown,getUp,getUpdate, getDel,createGoodsType} from '@/api/good'
import { getAdd,getFiles} from '@/api/file'
import waves from '@/directive/waves' // waves directive
import { parseTime } from '@/utils'
import Pagination from '@/components/GoodsPagination'
import fileDownload from 'js-file-download'
import checkPermission from '@/utils/permission' // 权限判断函数

export default {
  name: 'ComplexTable',
  components: { Pagination },
  directives: { waves },
  data() {
    return {
      typeId:null,
      value2:null,
      fileList:null,
      value: null,
      typeList: null,
      searchname:null,
      list: null,
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 8,
        searchName:null,
      },
      temp: {
       fileId:null,
       goodId:null,
       goodName:null,
       message:null,
       price:null,
       function:null,
       images:null,
       fee:null,
       up:null,
       down:null,
       goodsType:null
      },
      dialogFormVisible: false,
      dialogStatus: '',
      textMap: {
        update: '修改',
        up: '上架',
        down: '下架',
        file: '绑定文件',
        create:'添加资料类型'
      },
      dialogPvVisible: false,
      downloadLoading: false
    }
  },
  created() {
    this.getList(),this.getTypeList(),this.getFileList()
  },
  methods: {
     checkPermission,
     handleType(){
       this.typeId=this.value
       this.getList()
       this.typeId=null
     },

  getTypeList(){
  	findAllType().then(res=>{
  	  this.typeList=res.data
  	})
  },

  getFileList(){
  	getFiles().then(res=>{
  	  this.fileList=res.data
  	})
  },

    getList() {
      this.listLoading = true
      if(this.typeId==null){
        findGood1(this.listQuery).then(response => {
          this.list = response.data.items
          this.total = response.data.total

          setTimeout(() => {
            this.listLoading = false
          }, 1.5 * 1000)
        })
      }else{
        var query={
          "limit":this.listQuery.limit,
          "page":this.listQuery.page,
          "typeId":this.typeId
        }
        findGood2(query).then(response => {
          this.list = response.data.items
          this.total = response.data.total

          setTimeout(() => {
            this.listLoading = false
          }, 1.5 * 1000)
        })
      }

    },

    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },

    handleCreate(row) {
     this.temp = Object.assign({}, row) // copy obj
     this.dialogStatus = 'create'
     this.dialogFormVisible = true
     this.$nextTick(() => {
       this.$refs['dataForm'].clearValidate()
     })
    },
    createData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          createGoodsType(this.temp).then(() => {
            this.list.unshift(this.temp)
            this.getTypeList()
            this.dialogFormVisible = false
            this.$notify({
              title: 'Success',
              message: '添加类型成功！',
              type: 'success',
              duration: 2000
            })
          })
        }
      })
    },
    handleFiles(row){
      this.temp = Object.assign({}, row) // copy obj
      this.dialogStatus = 'file'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
      fileData() {

      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
           var fileParam={
             "goodId":this.temp.goodId,
             "fileId":this.value2
           }
          getAdd(fileParam).then(res => {
            this.list.unshift(this.temp)
            this.getTypeList()
            this.dialogFormVisible = false
            this.$notify({
              title: '绑定',
              message: res.data.msg,
              type: 'success',
              duration: 2000
            })
          })
        }
      })
    },
    handleUp(row) {
      this.temp = Object.assign({}, row)
      this.dialogStatus = 'up'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    upData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          const tempData = Object.assign({}, this.temp)
          // tempData.timestamp = +new Date(tempData.timestamp) // change Thu Nov 30 2017 16:41:05 GMT+0800 (CST) to 1512031311464
         getUp(tempData).then(res => {
            const index = this.list.findIndex(v => v.id === this.temp.id)
            this.list.splice(index, 1, this.temp)
            this.dialogFormVisible = false
              this. getList()
            this.$notify({
              title: '上架',
              message: res.status,
              type: 'success',
              duration: 2000
            })
          })
        }
      })
    },
    handleDown(row) {
      this.temp = Object.assign({}, row) // copy obj
      // this.temp.timestamp = new Date(this.temp.timestamp)
      this.dialogStatus = 'down'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    downData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          const tempData = Object.assign({}, this.temp)
          // tempData.timestamp = +new Date(tempData.timestamp) // change Thu Nov 30 2017 16:41:05 GMT+0800 (CST) to 1512031311464
         getDown(tempData).then(res => {
            const index = this.list.findIndex(v => v.id === this.temp.id)
            this.list.splice(index, 1, this.temp)
            this.dialogFormVisible = false
              this. getList()
            this.$notify({
              title: '下架',
              message: res.status,
              type: 'success',
              duration: 2000
            })
          })
        }
      })
    },
    handleUpdate(row) {
      this.temp = Object.assign({}, row) // copy obj
      // this.temp.timestamp = new Date(this.temp.timestamp)
      this.dialogStatus = 'update'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    updateData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          const tempData = Object.assign({}, this.temp)
          // tempData.timestamp = +new Date(tempData.timestamp) // change Thu Nov 30 2017 16:41:05 GMT+0800 (CST) to 1512031311464
         getUpdate(tempData).then(() => {
            const index = this.list.findIndex(v => v.id === this.temp.id)
            this.list.splice(index, 1, this.temp)
            this.dialogFormVisible = false
            this. getList()
            this.$notify({
              title: 'Success',
              message: '更新资料信息成功！',
              type: 'success',
              duration: 2000
            })
          })
        }
      })
    },
    handleDelete(row, index) {
       getDel(row.goodId).then(res=>{
         this.$notify({
           title: '删除',
           message: res.status,
           type: 'success',
           duration: 2000
         })
       })
       this. getList()
    },
    handleEs(){
      if(this.searchname==null||this.searchname==''){
         this.getList();
      }else{

       search(this.searchname).then(res=>{
          this.total=res.data.total,
          this.list=res.data.items
        })
      }

    },

    handleDownload() {
       getExport().them(res=>{

       })
    }

  }
}
</script>
